


package jnpf.zc.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.permission.entity.UserEntity;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import jnpf.zc.entity.ZcglJyghEntity;
import jnpf.zc.entity.ZcglZcdjInfoTzEntity;
import jnpf.zc.model.zcglzcdjinfotz.*;
import jnpf.zc.service.ZcglJyghService;
import jnpf.zc.service.ZcglZcdjInfoTzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * 资产台账
 *
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-12-19
 */
@Slf4j
@RestController
@Api(tags = "资产台账", value = "example")
@RequestMapping("/api/example/ZcglZcdjInfoTz")
public class ZcglZcdjInfoTzController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcglZcdjInfoTzService zcglZcdjInfoTzService;


    @Autowired
    private ZcglJyghService zcglJyghService;

    /**
     * 列表
     *
     * @param zcglZcdjInfoTzPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglZcdjInfoTzPagination zcglZcdjInfoTzPagination) throws IOException {
        List<ZcglZcdjInfoTzEntity> list = zcglZcdjInfoTzService.getList(zcglZcdjInfoTzPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<ZcglZcdjInfoTzListVO> listVO = JsonUtil.getJsonToList(list, ZcglZcdjInfoTzListVO.class);
        for (ZcglZcdjInfoTzListVO zcglZcdjInfoTzVO : listVO) {
            zcglZcdjInfoTzVO.setZcNumber_name(zcglZcdjInfoTzVO.getZcNumber());

            ZcglJyghEntity zcglJyghEntity = zcglZcdjInfoTzService.getZcglJygh(zcglZcdjInfoTzVO.getId());
            if (ObjectUtil.isNotEmpty(zcglJyghEntity)) {
                ZcglJyghListVO zcglJyghListVO = JsonUtil.getJsonToBean(zcglJyghEntity, ZcglJyghListVO.class);
                zcglJyghListVO.setOperatingCompany_name(zcglJyghListVO.getOperatingCompany());

                zcglJyghListVO.setProject_name(zcglJyghListVO.getProject());

                zcglJyghListVO.setModeOperation_name(generaterSwapUtil.getDicName(zcglJyghListVO.getModeOperation(), "600000000000000000"));

                zcglJyghListVO.setBusinessStatus_name(zcglJyghListVO.getBusinessStatus());

                zcglJyghListVO.setNotes_name(zcglJyghListVO.getNotes());

                zcglZcdjInfoTzVO.setZcgl_jygh(zcglJyghListVO);
            }
            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zcglZcdjInfoTzPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param zcglZcdjInfoTzForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcglZcdjInfoTzForm zcglZcdjInfoTzForm) throws DataException {
        boolean b = zcglZcdjInfoTzService.checkForm(zcglZcdjInfoTzForm, 0);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcglZcdjInfoTzEntity entity = JsonUtil.getJsonToBean(zcglZcdjInfoTzForm, ZcglZcdjInfoTzEntity.class);
        entity.setId(mainId);
        zcglZcdjInfoTzService.save(entity);

        //子表数据
        if (ObjectUtil.isNotEmpty(zcglZcdjInfoTzForm.getZcgl_jygh())) {
            ZcglJyghEntity zcgl_jyghentity = JsonUtil.getJsonToBean(zcglZcdjInfoTzForm.getZcgl_jygh(), ZcglJyghEntity.class);

            //自动生成的字段
            zcgl_jyghentity.setParentId(entity.getId());
            zcgl_jyghentity.setId(mainId);
            zcglJyghService.save(zcgl_jyghentity);
        }

        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglZcdjInfoTzInfoVO> info(@PathVariable("id") String id) {
        ZcglZcdjInfoTzEntity entity = zcglZcdjInfoTzService.getInfo(id);
        ZcglZcdjInfoTzInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglZcdjInfoTzInfoVO.class);

        //子表
        //副表
        QueryWrapper<ZcglJyghEntity> queryWrapperZcglJygh = new QueryWrapper<>();
        queryWrapperZcglJygh.lambda().eq(ZcglJyghEntity::getParentId, entity.getId());
        ZcglJyghEntity zcgl_jyghEntity = zcglJyghService.getOne(queryWrapperZcglJygh);
        ZcglJyghModel zcglJyghModel = JsonUtil.getJsonToBean(zcgl_jyghEntity, ZcglJyghModel.class);
        vo.setZcgl_jygh(zcglJyghModel);
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcglZcdjInfoTzInfoVO> detailInfo(@PathVariable("id") String id) {
        ZcglZcdjInfoTzEntity entity = zcglZcdjInfoTzService.getInfo(id);
        ZcglZcdjInfoTzInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglZcdjInfoTzInfoVO.class);

        //子表数据转换

        //附表数据转换
        QueryWrapper<ZcglJyghEntity> queryWrapperZcglJygh = new QueryWrapper<>();
        queryWrapperZcglJygh.lambda().eq(ZcglJyghEntity::getParentId, entity.getId());
        ZcglJyghEntity zcgl_jyghEntity = zcglJyghService.getOne(queryWrapperZcglJygh);
        if (ObjectUtil.isEmpty(zcgl_jyghEntity)) {
            zcgl_jyghEntity = new ZcglJyghEntity();
        }
        zcgl_jyghEntity.setOperatingCompany(zcgl_jyghEntity.getOperatingCompany());

        zcgl_jyghEntity.setProject(zcgl_jyghEntity.getProject());

        zcgl_jyghEntity.setModeOperation(generaterSwapUtil.getDicName(zcgl_jyghEntity.getModeOperation(), "600000000000000000"));

        zcgl_jyghEntity.setBusinessStatus(zcgl_jyghEntity.getBusinessStatus());

        zcgl_jyghEntity.setNotes(zcgl_jyghEntity.getNotes());

        ZcglJyghModel zcglJyghModel = JsonUtil.getJsonToBean(zcgl_jyghEntity, ZcglJyghModel.class);
        vo.setZcgl_jygh(zcglJyghModel);

//添加到详情表单对象中
        vo.setZcNumber(vo.getZcNumber());


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param zcglZcdjInfoTzForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZcglZcdjInfoTzForm zcglZcdjInfoTzForm) throws DataException {
        boolean b = zcglZcdjInfoTzService.checkForm(zcglZcdjInfoTzForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo = userProvider.get();
        ZcglZcdjInfoTzEntity entity = zcglZcdjInfoTzService.getInfo(id);
        if (entity != null) {
            ZcglZcdjInfoTzEntity subentity = JsonUtil.getJsonToBean(zcglZcdjInfoTzForm, ZcglZcdjInfoTzEntity.class);
            boolean b1 = zcglZcdjInfoTzService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            //子表数据
            if (ObjectUtil.isNotEmpty(zcglZcdjInfoTzForm.getZcgl_jygh())) {
                QueryWrapper<ZcglJyghEntity> queryWrapperZcglJygh = new QueryWrapper<>();
                queryWrapperZcglJygh.lambda().eq(ZcglJyghEntity::getParentId, entity.getId());
                ZcglJyghEntity zcgl_jyghOneEntity = zcglJyghService.getOne(queryWrapperZcglJygh);
                ZcglJyghEntity zcgl_jyghentity = JsonUtil.getJsonToBean(zcglZcdjInfoTzForm.getZcgl_jygh(), ZcglJyghEntity.class);
                zcgl_jyghentity.setId(zcgl_jyghOneEntity.getId());
                zcgl_jyghentity.setParentId(entity.getId());
                zcglJyghService.updateById(zcgl_jyghentity);
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZcglZcdjInfoTzEntity entity = zcglZcdjInfoTzService.getInfo(id);
        if (entity != null) {
            zcglZcdjInfoTzService.delete(entity);
            //子表数据
            QueryWrapper<ZcglJyghEntity> queryWrapperZcglJygh = new QueryWrapper<>();
            queryWrapperZcglJygh.lambda().eq(ZcglJyghEntity::getParentId, entity.getId());
            zcglJyghService.remove(queryWrapperZcglJygh);

        }
        return ActionResult.success("删除成功");
    }


}
